﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmNhapHang : Form
    {
        public event FormClosePhieuNhapKhoEventHandler formClosePhieuNhapKho = null;

        private BangKeNhapHangMgt bangkenhaphangmgt = new BangKeNhapHangMgt();
        private CTBangKeNhapHangMgt ctbangkenhaphangMgt = new CTBangKeNhapHangMgt();
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private DonGiaNguyenVatLieuManagement dongianguyenvatlieuMgt = new DonGiaNguyenVatLieuManagement();
        private DonViTinhMgt donvitinhMgt = new DonViTinhMgt();
        private List<CTBangKeNhapHang> lstDeleteCTBangKeNhapHang = new List<CTBangKeNhapHang>();

        private bool IsEdited = true;
        private bool IsUpdatePrice = false;

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        private BangKeNhapHang bangkenhaphang;
        public BangKeNhapHang BangKeNhapHang
        {
            set { this.bangkenhaphang = value; }
            get { return this.bangkenhaphang; }
        }

        private CTBangKeNhapHang ctbangkenhaphang;
        public CTBangKeNhapHang CTBangKeNhapHang
        {
            set { this.ctbangkenhaphang = value; }
            get { return this.ctbangkenhaphang; }
        }

        public FrmNhapHang(NhanVien nv)
        {
            InitializeComponent();

            this.nhanvien = nv;
            BindingToCbo();
            txtNguoiLap.Text = nhanvien.HoTen;

            groupBox1.Enabled = false;
            txtNguoiLap.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
        }        

        public FrmNhapHang(BangKeNhapHang bangkenhaphang, NhanVien nhanvien, bool IsUpdatePrice)
        {
            InitializeComponent();

            this.BangKeNhapHang = bangkenhaphang;
            this.NhanVien = nhanvien;

            BindingToCbo();
            txtNguoiLap.Enabled = false;
            groupBox1.Enabled = false;
            this.IsUpdatePrice = IsUpdatePrice;

            if (IsUpdatePrice)
            {
                CTBangKeNhapHang ctbangkenhaphang = new CTBangKeNhapHang();

                IsEdited = true;
                btnTaoMoi.Enabled = false;

                txtManhapPhieu.Text = this.bangkenhaphang.MaSo;
                txtNhanVienGiaoHang.Text = this.bangkenhaphang.NguoiGiaoHang;
                txtDiaChi.Text = this.bangkenhaphang.DiaChi;
                txtKeToanTruong.Text = this.bangkenhaphang.KeToanTruong;
                dtpNgayNhapHang.Text = this.bangkenhaphang.NgayThang.ToString();
                cboNhaCungCap.SelectedText = this.bangkenhaphang.NhaCungCap;
                txtNguoiLap.Text = this.bangkenhaphang.NguoiLap;
                labelID.Text = this.bangkenhaphang.Id.ToString();
                txtKeToanTruong.Text = this.nhanvien.HoTen;                

                ctbangkenhaphang.IdBangKe = this.bangkenhaphang.Id;
                dgvNhaphang.AutoGenerateColumns = false;
                dgvNhaphang.Columns["colDongia"].ReadOnly = false;
                dgvNhaphang.Columns["colMavatlieu"].ReadOnly = true;
                cboTenHang.ReadOnly = true;
                dgvNhaphang.Columns["cboDonvitinh"].ReadOnly = true;
                dgvNhaphang.Columns["colSoluong"].ReadOnly = true;
                dgvNhaphang.Columns["colKhoiluong"].ReadOnly = true;
                dgvNhaphang.DataSource = GetAllCTBangKeNhapHangByIdBangKe(ctbangkenhaphang);

                if (dgvNhaphang.Rows.Count <= 0)
                    btnLuu.Enabled = false;
            }
            else
            {
                txtManhapPhieu.Text = GetMaSoNhapHang();
            }
        }

        public FrmNhapHang(BangKeNhapHang bangkenhaphang, NhanVien nhanvien)
        {
            InitializeComponent();

            this.NhanVien = nhanvien;        
            this.bangkenhaphang = bangkenhaphang;

            BindingToCbo();
            btnLuu.Enabled = false;
            groupBox1.Enabled = false;
            dgvNhaphang.AutoGenerateColumns = false;

            txtManhapPhieu.Text =this.bangkenhaphang.MaSo;
            dtpNgayNhapHang.Value = this.bangkenhaphang.NgayThang;
            txtNhanVienGiaoHang.Text = this.bangkenhaphang.NguoiGiaoHang;
            cboNhaCungCap.Text = this.bangkenhaphang.NhaCungCap;
            txtDiaChi.Text = this.bangkenhaphang.DiaChi;
            txtNguoiLap.Text = this.bangkenhaphang.NguoiLap;
            txtKeToanTruong.Text = this.bangkenhaphang.KeToanTruong;
            labelID.Text = this.bangkenhaphang.Id.ToString();

            List<CTBangKeNhapHang> lstCTBangKeNhapHang = (from c in GetAllCTBangKeNhapHang()
                                                          where c.IdBangKe == bangkenhaphang.Id
                                                          select c).ToList();
            for (int i = 0; i < lstCTBangKeNhapHang.Count; i++)
            {
                DataGridViewRow row = new DataGridViewRow();

                DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
                List<HangHoa> lstHangHoa = GetAllHangHoa();
                cboCell.DataSource = lstHangHoa;
                cboCell.DisplayMember = "Ten";
                cboCell.ValueMember = "Id";
                cboCell.Value = lstCTBangKeNhapHang[i].TenVatLieu;
                row.Cells.Add(cboCell);
                
                DataGridViewCell cell = new DataGridViewTextBoxCell();
                cell.Value = lstCTBangKeNhapHang[i].MaVatLieu;
                row.Cells.Add(cell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.DataSource = GetAllDonViTinh();
                cboCell.DisplayMember = "TenDVT";
                cboCell.ValueMember = "TenDVT";
                cboCell.Value = lstCTBangKeNhapHang[i].DonViTinh;
                row.Cells.Add(cboCell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lstCTBangKeNhapHang[i].SoLuong;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lstCTBangKeNhapHang[i].KhoiLuong;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lstCTBangKeNhapHang[i].DonGia;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lstCTBangKeNhapHang[i].Id;
                row.Cells.Add(cell);

                dgvNhaphang.Rows.Add(row);
            }
        }

        private void btnTaoMoi_Click(object sender, EventArgs e)
        {           
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;

            groupBox1.Enabled = true;
            dgvNhaphang.Enabled = true;
            IsEdited = false;            

            if (dgvNhaphang.DataSource == null)
                dgvNhaphang.Rows.Clear();
            else
                dgvNhaphang.DataSource = null;

            cboNhaCungCap.SelectedIndex = 0;
            dtpNgayNhapHang.Value = DateTime.Now;

            txtNhanVienGiaoHang.Clear();
            txtManhapPhieu.Text = GetMaSoNhapHang();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (dgvNhaphang.Rows.Count <= 1 && !IsUpdatePrice)
            {
                MessageBox.Show("Bạn chưa nhập đầy đủ dữ liệu. !!!");
                return;
            }

            for (int i = 0; i < dgvNhaphang.Rows.Count - 1; i++)
            {
                if (dgvNhaphang.Rows[i].Cells[0].Value == null || dgvNhaphang.Rows[i].Cells[2].Value == null || dgvNhaphang.Rows[i].Cells[3].Value == null || dgvNhaphang.Rows[i].Cells[4].Value == null || dgvNhaphang.Rows[i].Cells[5].Value == null)
                {
                    MessageBox.Show("Vui lòng nhập thông tin trong danh sách !","Thông tin");
                    return;
                }
            }

            ResultObj result = new ResultObj();

            DateTime sngaythang = dtpNgayNhapHang.Value;
            string sMaNhapPhieu = txtManhapPhieu.Text;
            string sDiaChi = txtDiaChi.Text;
            string sNhanVienGiaoHang = txtNhanVienGiaoHang.Text;
            NhaCungCap nhacungcap = (NhaCungCap)cboNhaCungCap.SelectedItem;            

            BangKeNhapHang bangkenhaphang = new BangKeNhapHang();
            bangkenhaphang.MaSo = sMaNhapPhieu;
            bangkenhaphang.NhaCungCap = nhacungcap.TenNhaCungCap;
            bangkenhaphang.NguoiGiaoHang = sNhanVienGiaoHang;
            bangkenhaphang.DiaChi = sDiaChi;
            bangkenhaphang.NgayThang = sngaythang;
            bangkenhaphang.NguoiLap = txtNguoiLap.Text;
            bangkenhaphang.KeToanTruong = txtKeToanTruong.Text;
            bangkenhaphang.IsDelete = 0;

            //Insert
            if (!IsEdited)
            {
                result = bangkenhaphangmgt.SaveNewBangKeNhapHang(bangkenhaphang);
                labelID.Text = result.ReturnCode.ToString();

                ResultObj resultObj = new ResultObj();

                for (int i = 0; i < dgvNhaphang.Rows.Count - 1; i++)
                {
                    CTBangKeNhapHang ctbangkenhaphang = new CTBangKeNhapHang();
                    ctbangkenhaphang.IdBangKe = Convert.ToInt32(labelID.Text);
                    ctbangkenhaphang.TenVatLieu = dgvNhaphang.Rows[i].Cells[0].FormattedValue.ToString();
                    ctbangkenhaphang.MaVatLieu = dgvNhaphang.Rows[i].Cells[1].Value.ToString();
                    ctbangkenhaphang.KhoiLuong = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[4].Value);
                    ctbangkenhaphang.DonViTinh = dgvNhaphang.Rows[i].Cells[2].Value.ToString();
                    ctbangkenhaphang.SoLuong = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[3].Value);
                    ctbangkenhaphang.DonGia = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[5].Value);
                    ctbangkenhaphang.IsDelete = 0;

                    result = ctbangkenhaphangMgt.SaveNewCTBangKeNhapHang(ctbangkenhaphang);
                    dgvNhaphang.Rows[i].Cells[6].Value = result.ReturnCode;
                }
            }
            //Update
            else
            {
                bangkenhaphang.Id = Convert.ToInt32(labelID.Text);
                result = bangkenhaphangmgt.UpdateBangKeNhapHang(bangkenhaphang);

                for (int i = 0; i < dgvNhaphang.Rows.Count-1; i++)
                {
                    CTBangKeNhapHang ctbangkenhaphang = new CTBangKeNhapHang();

                    ctbangkenhaphang.Id = Convert.ToInt32(dgvNhaphang.Rows[i].Cells[6].Value);
                    ctbangkenhaphang.IdBangKe = bangkenhaphang.Id;
                    ctbangkenhaphang.TenVatLieu = dgvNhaphang.Rows[i].Cells[0].FormattedValue.ToString();
                    ctbangkenhaphang.MaVatLieu = dgvNhaphang.Rows[i].Cells[1].Value.ToString();
                    ctbangkenhaphang.KhoiLuong = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[4].Value);
                    ctbangkenhaphang.DonViTinh = dgvNhaphang.Rows[i].Cells[2].Value.ToString();
                    ctbangkenhaphang.SoLuong = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[3].Value);
                    ctbangkenhaphang.DonGia = Convert.ToInt64(dgvNhaphang.Rows[i].Cells[5].Value);

                    if (dgvNhaphang.Rows[i].Cells[6].Value != null)
                    {
                        ctbangkenhaphang.Id = Convert.ToInt32(dgvNhaphang.Rows[i].Cells[6].Value);
                        result = ctbangkenhaphangMgt.UpdateCTBangKeNhapHang(ctbangkenhaphang);
                    }
                    else
                    {
                        result = ctbangkenhaphangMgt.SaveNewCTBangKeNhapHang(ctbangkenhaphang);
                        dgvNhaphang.Rows[i].Cells[6].Value = result.ReturnCode;
                    }
                }

                IsEdited = false;
            }

            if (result.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");                

                if (IsUpdatePrice)
                {
                    this.bangkenhaphang.KeToanTruong = this.nhanvien.HoTen;
                    formClosePhieuNhapKho();
                }
                //delete cac CTPhieuNhapKho do nguoi dung chuot phai "XOA"
                if (lstDeleteCTBangKeNhapHang.Count > 0)
                {
                    foreach (CTBangKeNhapHang ct in this.lstDeleteCTBangKeNhapHang)
                    {
                        ct.IsDelete = 1;
                        ctbangkenhaphangMgt.UpdateCTBangKeNhapHang(ct);
                    }
                }

                
                btnLuu.Enabled = false;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
            }
            else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
            {
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                if (!IsEdited)
                {
                    bangkenhaphang.Id = Convert.ToInt32(labelID.Text);
                    bangkenhaphangmgt.DeleteBangKeNhapHang(bangkenhaphang);
                }
            }
            else
            {
                MessageBox.Show("Thất bại");
                if (!IsEdited)
                {
                    bangkenhaphang.Id = Convert.ToInt32(labelID.Text);
                    bangkenhaphangmgt.DeleteBangKeNhapHang(bangkenhaphang);
                }
            }
        }     

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboNhaCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            NhaCungCap nhacungcap = (NhaCungCap)cboNhaCungCap.SelectedItem;
            txtDiaChi.Text = nhacungcap.DiaChi;
        }

        private void dgvNhaphang_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }      

        private void btnSua_Click(object sender, EventArgs e)
        {
            dgvNhaphang.Enabled = true;
            btnLuu.Enabled = true;

            IsEdited = true;
            groupBox1.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo!", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                ResultObj result = new ResultObj();

                List<CTBangKeNhapHang> lstctbangkenhaphang = (from ct in GetAllCTBangKeNhapHang()
                                                              where ct.IdBangKe == bangkenhaphang.Id
                                                              select ct).ToList();

                bangkenhaphang.Id = Convert.ToInt32(labelID.Text);
                bangkenhaphang.IsDelete = 1;
                IsEdited = true;
                result = bangkenhaphangmgt.UpdateBangKeNhapHang(bangkenhaphang);

                for (int i = 0; i < lstctbangkenhaphang.Count; i++)
                {
                    lstctbangkenhaphang[i].IsDelete = 1;
                    result = ctbangkenhaphangMgt.UpdateCTBangKeNhapHang(lstctbangkenhaphang[i]);
                }
                
                txtManhapPhieu.Clear();
                txtDiaChi.Clear();
                txtNhanVienGiaoHang.Clear();

                dtpNgayNhapHang.Value = DateTime.Now;
                dgvNhaphang.DataSource = null;
                dgvNhaphang.ClearSelection();
            }
        }

        public string GetMaSoNhapHang()
        {
            ResultObj resultObj = new ResultObj();
            List<BangKeNhapHang> lstbangkenhaphang = new List<BangKeNhapHang>();

            DateTime dt = DateTime.Now;
            string sResult = @"BK/";

            resultObj = bangkenhaphangmgt.GetAllBangKeNhapHangToCheckCode();
            lstbangkenhaphang = (List<BangKeNhapHang>)resultObj.Result;

            int month = dt.Month;
            int year = dt.Year;
            string sNewnumber = "";

            int newMaSo = 0;

            if (lstbangkenhaphang.Count == 0)
                newMaSo = 1;
            else
            {
                //kiem tra da thay doi thang moi chua
                int index = lstbangkenhaphang[lstbangkenhaphang.Count - 1].MaSo.IndexOf('/');
                int test = Convert.ToInt32(lstbangkenhaphang[lstbangkenhaphang.Count - 1].MaSo.Substring(index + 1, 2).ToString());
                if (test != month)
                    newMaSo = 1;
                else
                {
                    index = lstbangkenhaphang[lstbangkenhaphang.Count - 1].MaSo.LastIndexOf('/');
                    int so = Convert.ToInt32(lstbangkenhaphang[lstbangkenhaphang.Count - 1].MaSo.Substring(index + 1).ToString());
                    newMaSo = so + 1;
                }
            }
            string sMonth = month.ToString();
            string sYear = year.ToString();

            if (sMonth.Length == 1)
                sMonth = "0" + sMonth;

            sYear = sYear.Substring(2);
            

            string sId = newMaSo.ToString();
            if (sId.Length == 1)
                sNewnumber = "0000" + sId;
            else if (sId.Length == 2)
                sNewnumber = "000" + sId;
            else if (sId.Length == 3)
                sNewnumber = "00" + sId;
            else if (sId.Length == 4)
                sNewnumber = "0" + sId;
            else
                sNewnumber = sId;

            sResult += sMonth + sYear + "/" + sNewnumber;

            return sResult;
        }

        private void BindingToCbo()
        {
            cboNhaCungCap.DataSource = GetAllNhaCungCap();
            cboNhaCungCap.DisplayMember = "TenNhaCungCap";
            cboNhaCungCap.ValueMember = "Id";

            cboTenHang.DataSource = GetAllHangHoa();
            cboTenHang.DisplayMember = "Ten";
            cboTenHang.ValueMember = "Id";

            cboDonvitinh.DataSource = GetAllDonViTinh();
            cboDonvitinh.DisplayMember = "TenDVT";
            cboDonvitinh.ValueMember = "TenDVT";
        }

        private void cboNhaCungCap_Leave(object sender, EventArgs e)
        {
            foreach (NhaCungCap b in cboNhaCungCap.Items)
            {
                if (b.TenNhaCungCap.ToLower().Contains(cboNhaCungCap.Text))
                {
                    cboNhaCungCap.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboNhaCungCap.SelectedValue == null)
            {
                cboNhaCungCap.Text = "";
                cboNhaCungCap.Focus();
            }
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvNhaphang.CurrentRow != null)
            {
                if (dgvNhaphang.CurrentRow.Index != (dgvNhaphang.Rows.Count))
                {
                    if (dgvNhaphang.CurrentRow.Cells[6].Value != null)
                    {
                        int id = Convert.ToInt32(dgvNhaphang.CurrentRow.Cells[6].Value);
                        CTBangKeNhapHang ctBangKeNhapHang = (from ct in GetAllCTBangKeNhapHang()
                                                         where ct.Id == id
                                                         select ct).FirstOrDefault();
                        if (ctBangKeNhapHang != null)
                        {
                            if (!lstDeleteCTBangKeNhapHang.Contains(ctBangKeNhapHang))
                                lstDeleteCTBangKeNhapHang.Add(ctBangKeNhapHang);

                        }
                    }

                    dgvNhaphang.Rows.RemoveAt(dgvNhaphang.CurrentRow.Index);
                }
            }

        }

        private void dgvNhaphang_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.ColumnIndex == 2)
                {
                    if (dgvNhaphang.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
                    {
                        MessageBox.Show("Vui lòng chọn đơn vị tính !", "Thông báo");
                        return;
                    }
                }

                if (e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5)
                {
                    long num;
                    if (dgvNhaphang.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !Int64.TryParse(dgvNhaphang.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out num))
                    {
                        MessageBox.Show("Vui lòng nhập số !", "Thông báo");
                        dgvNhaphang.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
                        dgvNhaphang.Focus();
                    }
                }

                if (e.ColumnIndex == 0)
                {
                    HangHoa hanghoa = (from hh in GetAllHangHoa()
                                       where hh.Id == Convert.ToInt32(dgvNhaphang.Rows[e.RowIndex].Cells[0].Value.ToString())
                                       select hh).FirstOrDefault();

                    if (hanghoa.Id > -1)
                    {
                        dgvNhaphang.Rows[e.RowIndex].Cells[1].Value = hanghoa.MaSo;

                        NhaCungCap nhacungcap = (NhaCungCap)cboNhaCungCap.SelectedItem;

                        DonGiaNguyenVatLieu dongiavatlieu = new DonGiaNguyenVatLieu();
                        dongiavatlieu.Id = 0;
                        dongiavatlieu.IdNhaCungCap = nhacungcap.Id;
                        dongiavatlieu.IdHangHoa = Convert.ToInt32(dgvNhaphang.Rows[e.RowIndex].Cells[0].Value);

                        List<DonGiaNguyenVatLieu> dongianguyenvatlieu = (List<DonGiaNguyenVatLieu>)dongianguyenvatlieuMgt.GetDonGiaNguyenVatLieu(dongiavatlieu).Result;

                        if (dongianguyenvatlieu.Count > 0)
                            dgvNhaphang.Rows[e.RowIndex].Cells[5].Value = dongianguyenvatlieu[0].DonGia;
                        else
                            dgvNhaphang.Rows[e.RowIndex].Cells[5].Value = hanghoa.DonGia;
                    }
                    else
                        dgvNhaphang.Rows[e.RowIndex].Cells[0].Value = null;
                }
            }
        }

        private void dgvNhaphang_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvNhaphang.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvNhaphang.ClearSelection();
                    dgvNhaphang.CurrentCell = dgvNhaphang.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvNhaphang.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        public List<DonViTinh> GetAllDonViTinh()
        {
            ResultObj resultObj = new ResultObj();
            List<DonViTinh> lstDVT = new List<DonViTinh>();

            resultObj = donvitinhMgt.GetAllDonViTinh();
            lstDVT = (List<DonViTinh>)resultObj.Result;

            return lstDVT;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj result = new ResultObj();

            result = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> listtenhang = (from hh in(List<HangHoa>)result.Result
                                             where hh.IsDelete==0
                                             select hh).ToList();

            return listtenhang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj result = new ResultObj();

            result = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> lstNhaCungCap = (from ncc in(List<NhaCungCap>)result.Result
                                                  where ncc.IsDelete==0
                                                  select ncc).ToList();

            return lstNhaCungCap;
        }

        public List<DonGiaNguyenVatLieu> GetAllDonGiaNguyenVatLieu()
        {
            ResultObj result = new ResultObj();

            result = dongianguyenvatlieuMgt.GetAllDonGiaNguyenVatLieu();
            List<DonGiaNguyenVatLieu> listDonGiaNguyenVatLieu = (from dg in(List<DonGiaNguyenVatLieu>)result.Result
                                                                     where dg.IsDelete==0
                                                                     select dg).ToList();

            return listDonGiaNguyenVatLieu;
        }

        public List<CTBangKeNhapHang> GetAllCTBangKeNhapHangByIdBangKe(CTBangKeNhapHang ctbangkenhaphang)
        {
            ResultObj obj = new ResultObj();

            obj = ctbangkenhaphangMgt.GetAllCTBangKeNhapHangByIdBangKe(ctbangkenhaphang);
            List<CTBangKeNhapHang> listCTBangKeNhapHang = (from ct in(List<CTBangKeNhapHang>)obj.Result
                                                               where ct.IsDelete==0
                                                               select ct).ToList();

            return listCTBangKeNhapHang;
        }

        public List<CTBangKeNhapHang> GetAllCTBangKeNhapHang()
        {
            ResultObj result = new ResultObj();

            result = ctbangkenhaphangMgt.GetAllCTBangKeNhapHang();
            List<CTBangKeNhapHang> listCTBangKeNhapHang = (from ct in(List<CTBangKeNhapHang>)result.Result
                                                               where ct.IsDelete==0
                                                               select ct).ToList();

            return listCTBangKeNhapHang;        
        }
    }
}